package Sprite;

import Core.AnimateType;
import Core.GameTime;
import Core.SpriteBatch;
import Core.Vector2;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;

public class Sprite {

	protected Bitmap image;
	protected Paint paint;
	protected Point frameSize;
    Point currentFrame;
    protected Point sheetSize;
	int timeSinceLastFrame = 0;
    int millisecondsPerFrame = 16;
    protected float scale = 1;
    protected Vector2 speed;
    protected Vector2 position;
    protected Matrix matrix;
    protected Point origin;
    protected AnimateType animateType;
    private int animateSign = 1; // use for animate circle
    public Sprite(Bitmap image, Point frameSize, Point sheetSize, Point currentFrame, 
    		int millisecondsPerFrame, float scale, Vector2 speed, Vector2 position){
    	this.image = image;
    	this.frameSize = frameSize;
    	this.sheetSize = sheetSize;
    	this.currentFrame = currentFrame;
    	this.origin = new Point(0,0);
    	this.matrix = new Matrix();
    	this.paint = new Paint();
    	this.speed = speed;
    	this.position = position;
    	this.scale = scale;
    	paint.setColor(Color.rgb(255, 255, 255));
    }
    protected float getWidth() {
		return image.getWidth()*scale;
		
	}
    protected float getHeight() {
		return image.getHeight()*scale;
	}
	public void draw(GameTime gameTime, SpriteBatch spriteBatch){
		RectF dst = new RectF(position.x,position.y, position.x + frameSize.x*scale, 
				position.y+frameSize.y*scale);
		Rect src = new Rect(currentFrame.x * frameSize.x, currentFrame.y * frameSize.y,
				frameSize.x, frameSize.y);
		spriteBatch.Draw(image, src, dst, paint);		
	}
	
	public void update(GameTime gameTime){
		timeSinceLastFrame += gameTime.getElapsedTime();
        if (timeSinceLastFrame > millisecondsPerFrame)
        {
            timeSinceLastFrame = 0;
            switch (animateType.getType())
            {
                case AnimateType.NORMAL:
                    ++currentFrame.x;
                    if (currentFrame.x >= sheetSize.x)
                    {
                        currentFrame.x = 0;
                        ++currentFrame.y;
                        if (currentFrame.y >= sheetSize.y)
                            currentFrame.y = 0;
                    }
                    break;
                case AnimateType.INVERT:
                    --currentFrame.x;
                    if (currentFrame.x < 0)
                    {
                        currentFrame.x = sheetSize.x - 1;
                        --currentFrame.y;
                        if (currentFrame.y <0 )
                            currentFrame.y = sheetSize.y -1;
                    }
                    break;
                case AnimateType.CIRCLE:
                    currentFrame.x+=animateSign;
                    if (currentFrame.x >= sheetSize.x)
                    {
                        currentFrame.x = 0;
                        ++currentFrame.y;
                        if (currentFrame.y >= sheetSize.y)
                        {
                            currentFrame.y = sheetSize.y - 2 ;
                            currentFrame.x = sheetSize.x -1 ;
                            animateSign *= -1;
                        }
                    }
                    else if (currentFrame.x < 0)
                    {
                        currentFrame.x = sheetSize.x - 1;
                        --currentFrame.y;
                        if (currentFrame.y < 0)
                        {
                            currentFrame.y = 1;
                            currentFrame.x = 0;
                            animateSign *= -1;
                        }
                    }
                    break;
            }            
            
        }
	}
	
	protected void rotate(float degrees){
		matrix.setRotate(degrees, origin.x, origin.y);
	}
	
	public Rect collisionRect(int left, int top, int right, int bottom){
		return new Rect(left,top,right,bottom);
	}
	
	public Rect collisionRect(int collisionOffset){
		return new Rect((int)position.x + collisionOffset,
                (int)position.y + collisionOffset,
                (int)(frameSize.x * scale  - (collisionOffset)),
                (int)(frameSize.y * scale  - (collisionOffset)));
	}
}
